// src/api/card.ts
import type { activityCard, missionCard } from '@/types/card'

// 模拟数据存储
let activityCards: activityCard[] = []
let missionCards: missionCard[] = []

// 生成唯一ID
const generateId = (): string => {
  return Date.now().toString() + Math.random().toString(36).substr(2, 9)
}

// ActivityCard API
export const activityCardApi = {
  // 获取所有活动卡片
  getAll(): Promise<activityCard[]> {
    return Promise.resolve([...activityCards])
  },

  // 根据ID获取活动卡片
  getById(id: string): Promise<activityCard | null> {
    const card = activityCards.find(card => card.id === id)
    return Promise.resolve(card || null)
  },

  // 根据日期获取活动卡片
  getByDate(date: Date): Promise<activityCard[]> {
    const dateString = date.toDateString()
    const cards = activityCards.filter(card => 
      card.date.toDateString() === dateString
    )
    return Promise.resolve(cards)
  },

  // 创建活动卡片
  create(cardData: Omit<activityCard, 'id'>): Promise<activityCard> {
    const newCard: activityCard = {
      ...cardData,
      id: generateId(),
      createdAt: new Date(),
      updatedAt: new Date()
    }
    activityCards.push(newCard)
    return Promise.resolve(newCard)
  },

  // 更新活动卡片
  update(id: string, cardData: Partial<Omit<activityCard, 'id'>>): Promise<activityCard | null> {
    const index = activityCards.findIndex(card => card.id === id)
    if (index === -1) return Promise.resolve(null)

    activityCards[index] = {
      ...activityCards[index],
      ...cardData,
      id,
      updatedAt: new Date()
    }

    return Promise.resolve(activityCards[index]!)
  },

  // 删除活动卡片
  delete(id: string): Promise<boolean> {
    const index = activityCards.findIndex(card => card.id === id)
    if (index === -1) return Promise.resolve(false)
    
    activityCards.splice(index, 1)
    return Promise.resolve(true)
  },

  // 清空所有活动卡片
  clear(): Promise<void> {
    activityCards = []
    return Promise.resolve()
  }
}

// MissionCard API
export const missionCardApi = {
  // 获取所有任务卡片
  getAll(): Promise<missionCard[]> {
    return Promise.resolve([...missionCards])
  },

  // 根据ID获取任务卡片
  getById(id: string): Promise<missionCard | null> {
    const card = missionCards.find(card => card.id === id)
    return Promise.resolve(card || null)
  },

  // 根据日期获取任务卡片
  getByDate(date: Date): Promise<missionCard[]> {
    const dateString = date.toDateString()
    const cards = missionCards.filter(card => 
      card.date.toDateString() === dateString
    )
    return Promise.resolve(cards)
  },

  // 创建任务卡片
  create(cardData: Omit<missionCard, 'id'>): Promise<missionCard> {
    const newCard: missionCard = {
      ...cardData,
      id: generateId(),
      isFinished: false, // 默认未完成
      createdAt: new Date(),
      updatedAt: new Date()
    }
    missionCards.push(newCard)
    return Promise.resolve(newCard)
  },

  // 更新任务卡片
  update(id: string, cardData: Partial<Omit<missionCard, 'id'>>): Promise<missionCard | null> {
    const index = missionCards.findIndex(card => card.id === id)
    if (index === -1) return Promise.resolve(null)
    
    missionCards[index] = {
      ...missionCards[index],
      ...cardData,
      id,
      updatedAt: new Date()
    }
    
    return Promise.resolve(missionCards[index]!)
  },

  // 删除任务卡片
  delete(id: string): Promise<boolean> {
    const index = missionCards.findIndex(card => card.id === id)
    if (index === -1) return Promise.resolve(false)
    
    missionCards.splice(index, 1)
    return Promise.resolve(true)
  },

  // 清空所有任务卡片
  clear(): Promise<void> {
    missionCards = []
    return Promise.resolve()
  }
}